<!--#include file="../nav/admin_top.php" -->

<style type="text/css">
/* 购物车商品 */
.combo_name{
font-size: 14px;
font-weight: bold;
margin: 10px auto;
color: #555555;
}

.vegeitem{
border: 1px dashed #CCCCCC;
color: #006600;
float: left;
margin-bottom: 10px;
margin-right: 10px;
padding: 5px 10px;
}

/* 购物车表格 */
table.carttb{
border-collapse:collapse;
width:100%;
}
table.carttb th{
color:#666666;
border-bottom: 1px solid #CECECE;
text-align:center;
padding:10px;
background-color:#E8EEF1;
}
table.carttb td{
color:#666666;
border-bottom: 1px dotted #CECECE;
text-align:center;
vertical-align:middle;
padding:5px;
}
table.carttb td.l{
text-align:left;
}
table.carttb td.r{
text-align:right;
}

input.numtext{
width:50px;
text-align:center;
}
/* 选菜份数增加减少按钮 */
.numclick{
background-color:#C3CDDC;
color:#606E7B;
border:1px solid #929CA7;
cursor:pointer;
} 
</style>
<div class="titleDiv">
	<div class="titleCaption">
		<span class="captionSpan">新增订单 </span>
	</div>
</div> 
<div class="blockdiv">
	<div class="titlediv">1 选择会员</div>
	<table class="mytable" >
		<tr>
			<td class="mylabel">会员：</td>
			<td class="myinfo" >
				<span id="slt_user_span"> </span>
				<span id="del_user_span" style="display:none"><a href="javascript:void(0)"  onclick="fnDelUser()">清除</a> 或 </span>
				<a href="javascript:void(0)" id="slt_user_a"  onclick="fnGetUser()" title="请点击选择">选择会员</a>
				<input type="hidden" id="user_id" name="user_id" value="" />
			</td>
		</tr>
	</table>
</div>
<div class="blockdiv">
	<div class="titlediv">2 选择菜品套餐</div>
	<div class="margin10">
		<a href="javascript:void(0)"  onclick="fnGetCombo()" title="请点击选择">选择菜品套餐</a>
	</div>
	<div class="margin20">
		<table class="carttb">
			<thead>
				<Tr>
					<Th style="width:350px;">商品</Th>
					<Th>单价</Th>
					<Th>数量（套）</Th>
					<Th>小计</Th>
					<Th>操作</Th>
				</Tr>
			</thead>
			<tbody id="carttbody">
			</tbody>
		</table>
	</div>	
	<div class="margin10"  >
		<table style="border-collapse:collapse;width:100%">
			<Tr> 
				<td style="text-align:right">
					<div style="float:right">商品总计（不含运费）：<span style="font-weight:bold;font-size:16px;color:#FF3333">￥</span><span id="price_all" style="font-weight:bold;font-size:16px;color:#FF3333">0.00</span>
					</div> 
				</td>
			</Tr>
		</table>
	</div>
</div>
<div class="blockdiv">
	<div class="titlediv">3 配送日期</div>
	<div class="margin10">
		<table class="mytable" >
			<tr>
				<td class="mylabel">配送日期：</td>
				<td class="myinfo" >
					<input type="text" class="textBoxDate" id="rec_date" value=""   maxlength="10" />
					<input id="rec_date_slt" class="btnicon_calendar" type="button">
				</td>
			</tr>
			<tr>
				<td class="mylabel">配送时间段：</td>
				<td class="myinfo" >
					<select id="sendtime" class="textBox" name="sendtime">
						<option value="" selected="selected">-- 请选择配送时间段 --</option>
						<?PHP
						foreach($sendtime as $v){
							echo '<option value="'.$v['id'].'">'.$v['val'].'</option>';
						}
						?>
					</select>
				</td>
			</tr>
		</table>
	</div>
</div>
<div  class="button_div">
	<button class="btn_save" onclick="fnSave()">提 交</button>
</div>  
<script type="text/javascript">
var url_save = '/admin_order/add';
var url_return = '/admin_order/add';
var nowindex=0;
var rs_veges = {json_encode($rs_veges)};
var cache_veges = {};

$().ready(function(){  
	showLoading();   
	o2js.build('jscalendar');
	o2js.build('dates');
	fnSetSltDate('rec_date','rec_date_slt');

});
 
/* 保存 */
function fnSave(){ 
	var rec_date = $('#rec_date').val();
	var sendtime = $('#sendtime').val();
	var user_id = $("#user_id").val();
	var combos = [];
	
	$('.cart_tr').each(function(){
		var nowindexs = $(this).attr('nowindexs');
		var combo_id = $(this).attr('combo_id');
		var combo_type = $(this).attr('combo_type');
		var nums = $('#nums_'+nowindexs).val();
		
		if(combo_type == '0'){
			combos.push({
				combo_id:combo_id,
				nums:nums,
				cart_item:[]
			});
		}else{
			combos.push({
				combo_id:combo_id,
				nums:nums,
				cart_item:cache_veges[nowindexs]
			});
		}
	});
	 
	/* 一些验证 */
	if( user_id == '' ){
		alert('请选择会员');
		//fnGetUser();
		return false;
	}
	if( rec_date == '' ){
		alert('请选择配送日期');
		//fnGetUser();
		return false;
	}
	if( sendtime == '' ){
		alert('请选择配送时间段');
		//fnGetUser();
		return false;
	}
	if( combos.length <= 0 ){
		alert('请选择菜品套餐');
		//fnGetCombo();
		return false;
	}
	 
	$('button').attr('disabled','disabled');
	$.post(
		url_save,
		{
			user_id:user_id,
			rec_date:rec_date,
			sendtime:sendtime,
			combos:JSON.encode(combos)
		},
		function(e){
			var ok = errorHandler(e);
			if(ok && ok.success=='1'){
				alert('操作成功！');
				jumpTo(url_return);
			}
			$('button').attr('disabled','');
		}
	);
	return false;
}
 
/* 选择套餐 */
function fnGetCombo(){
	if(typeof ymPrompt == 'undefined') 	o2js.build('ymprompt');	
	ymPrompt.close();
	ymPrompt.win({
		title:'选择菜品套餐',
		fixPosition:true,
		maxBtn:true,
		width: 950, 
		height: 530,
		iframe:{
			src:'/selection/vegecombo_byadmin/?funcname=fnGetComboEnd'
		}
	});
}
function  fnGetComboEnd(e){  
	if(e.combo_type=="0"){
		var nocombo = true;
		$('.cart_tr').each(function(){
			var combo_id = $(this).attr('combo_id');
			if(e.combo_id == combo_id){
				fnAdd( $(this).attr('nowindexs') );
				nocombo = false;
			}
		});
		if(nocombo){
			$.post(
				'/admin_order/add_load_combo',
				{
					id:e.combo_id
				},
				function(e){
					var ok = errorHandler(e);
					if(ok && ok.success=='1'){ 
						var obj = ok.rs;
						var html = '';
						html += '<tr class="cart_tr" id="cart_tr_'+nowindex+'" nowindexs="'+nowindex+'" combo_type="'+obj.type+'" price="'+obj.price+'" combo_id="'+obj.id+'" >';
						html += '<td class="l" >';
						html += '	<div class="combo_name">'+obj.name+'</div>';
						html += '	<div class="combo_veges"> '; 
						for(var ii=0; ii<obj.combo_item.length; ii++){
							html += '		<div class="vegeitem">';
							html += obj.combo_item[ii].vege_name;
							html += '			<span> '+obj.combo_item[ii].vege_num+' 份</span>';
							html += '		</div>  ';
						} 
						html += '		<div class="clear"></div>';
						html += '	</div>';
						html += '	<div class="combo_senddate">可配送时间：'+obj.sendbdate+' 至 '+obj.sendedate+'</div>';
						html += '</td>';
						html += '<td  >';
						html += obj.price;
						html += '</td>';
						html += '<td>';
						html += '	<button class="numclick" onclick="fnShao('+nowindex+')" title="点击减少商品数量">-</button>';
						html += '	<input type="text"  value="1" id="nums_'+nowindex+'" readonly="readonly" class="numtext">';
						html += '	<button class="numclick" onclick="fnAdd('+nowindex+')"  title="点击增加商品数量">+</button>'; 
						html += '</td>';
						html += '<td style="font-weight:bold" id="price_total_'+nowindex+'" >';
						html += '	0.00'; 
						html += '</td>';
						html += '<td>';
						html += '	<a href="javascript:void(0)" class="dela" onclick="fnDel('+nowindex+')">删除</a>';
						html += '</td>';
						html += '</tr>'; 
						$('#carttbody').append(html);
						nowindex++;
						//fnLoadSendDate();
					}
				}
			);
		}
	}else{  
		var price_total = 0;
		cache_veges[nowindex] = e.veges;
		for(var k=0; k<e.veges.length; k++){
			price_total += parseInt(e.veges[k]['vege_num'],10) * rs_veges[e.veges[k].vege_id]['price'];
		}
		var html = '';
		html += '<tr class="cart_tr" id="cart_tr_'+nowindex+'" nowindexs="'+nowindex+'" combo_type="1" price="'+price_total+'" combo_id="'+e.combo_id+'" >';
		html += '<td class="l" >';
		html += '	<div class="combo_name">'+e.combo_name+'</div>';
		html += '	<div class="combo_veges"> '; 
		for(var ii=0; ii<e.veges.length; ii++){
			html += '		<div class="vegeitem">';
			html += rs_veges[e.veges[ii].vege_id]['name'];
			html += '			<span> '+e.veges[ii].vege_num+' 份</span>';
			html += '		</div>  ';
		} 
		html += '		<div class="clear"></div>';
		html += '	</div>';
		html += '	<div class="combo_senddate">可配送时间：'+e.combo_sendbdate+' 至 '+e.combo_sendedate+'</div>';
		html += '</td>';
		html += '<td  >';
		html += price_total;
		html += '</td>';
		html += '<td>';
		html += '	<button class="numclick" onclick="fnShao('+nowindex+')" title="点击减少商品数量">-</button>';
		html += '	<input type="text"  value="1" id="nums_'+nowindex+'" readonly="readonly" class="numtext">';
		html += '	<button class="numclick" onclick="fnAdd('+nowindex+')"  title="点击增加商品数量">+</button>'; 
		html += '</td>';
		html += '<td style="font-weight:bold" id="price_total_'+nowindex+'" >';
		html += '	0.00'; 
		html += '</td>';
		html += '<td>';
		html += '	<a href="javascript:void(0)" class="dela" onclick="fnDel('+nowindex+')">删除</a>';
		html += '</td>';
		html += '</tr>'; 
		$('#carttbody').append(html);
		nowindex++; 
		//fnLoadSendDate();
	}
}
 

/* 清楚会员选择 */
function fnDelUser(){
	$('#user_id').val('');
	$('#slt_user_span').html(' ');
	$('#slt_user_a').html('选择会员');
	$('#del_user_span').hide();
}
/* 选择会员 */
function fnGetUser(){
	GfnSelectUser('onFnGetUser','选择会员');
}
function onFnGetUser(e){
	if(e.user_ids.length > 1){
		alert('一次只能为一个会员下订单，请重新选择');
		fnGetUser();
		return false;
	}
  $('#user_id').val('');
	$('#slt_user_span').html(' ');
  $('#user_id').val(e.user_ids[0].id);
	$('#slt_user_span').html('('+e.user_ids[0].username+')'+e.user_ids[0].name);
	$('#slt_user_a').html('更换用户');
	$('#del_user_span').show(); 
} 




$().ready(function(){
	setInterval(function(){
		fnCaclPrice();
	},100);
});
/* 重新计算价格 */
function fnCaclPrice(){
	var price_all=0;
	$('.cart_tr').each(function(){
		var nowindexs=$(this).attr('nowindexs');
		var price=$(this).attr('price');
		var num=parseInt($('#nums_'+nowindexs).val(),10);
		var price_total = num*price;
		$('#price_total_'+nowindexs).html('￥ '+fnDec(price_total,2));
		price_all += price_total;
	});
	$('#price_all').html(fnDec(price_all,2));
}
/* 删除单个 */
function fnDel(id){
	fnConfirm({
		str:"确认删除？",
		func_true:function(){
			  $('#cart_tr_'+id).remove();
				//fnLoadSendDate();
		}
	});
}
function fnShao(id){
	var vege_num =  parseInt(strToNum($('#nums_'+id).val()),10);
	var new_vege_num = vege_num - 1;
	if(new_vege_num <= 0){
		new_vege_num = 1;
	}
	$('#nums_'+id).val(new_vege_num);
}
function fnAdd(id){
	var vege_num =  parseInt(strToNum($('#nums_'+id).val()),10);
	var new_vege_num = vege_num + 1;
	if(new_vege_num <= 0){
		new_vege_num = 1;
	}
	$('#nums_'+id).val(new_vege_num);
}
</script>
<!--#include file="../nav/admin_bottom.php" -->